package com.logic.modular.game.battle.process.battleStage.actionPhase.actionPhase2;

import com.logic.comment.util.BeanUtil;
import com.logic.modular.game.battle.panel.Battle;
import com.logic.modular.game.battle.panel.attribute.domain.PlayerHeroAttribute;
import com.logic.modular.game.battle.panel.attribute.PlayerHeroAttributeServiceImpl;
import com.logic.modular.game.battle.panel.skillEffect.PvpSkillEffect;
import com.logic.modular.game.battle.panel.skillEffect.PvpSkillEffectServiceImpl;
import com.logic.modular.game.battle.process.domain.Reason;
import com.logic.modular.game.battle.process.skillEffectHandle.impl.OrdinaryAttackStrategy;
import com.logic.modular.game.battle.process.battleStage.actionPhase.actionPhase2.item.ReleaseSkillService;
import com.logic.modular.game.battle.process.domain.ActionInfo;
import com.logic.modular.game.battle.process.skillEffectHandle.skillEffectSelect.SkillEffectTriggerHandle;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 2
 * 武将行动阶段
 */
@Slf4j
@Service
public class ActionStageService {
    @Resource
    private PlayerHeroAttributeServiceImpl attributeService;
    @Resource
    private PvpSkillEffectServiceImpl skillEffectService;
    @Resource
    private ReleaseSkillService releaseSkillService;
    @Resource
    private OrdinaryAttackStrategy ordinaryAttack;

    public ActionInfo actionApply(
            String battleUUID,
            PlayerHeroAttribute actWJ
    ) {
        List<PlayerHeroAttribute> targetRange = attributeService.obtainSurvivalHeroList(battleUUID);
        log.info("==================武将行动阶段,{}{}{}",
                actWJ.getUid(), actWJ.getName(), actWJ.getCurrentForce());
        Battle battle = Battle.get(battleUUID);
        Long battleId = battle.getBattleId();

        ActionInfo actionInfo = new ActionInfo();

        PlayerHeroAttribute attribute = attributeService.get(battleUUID, actWJ.getId());
        attribute.setPhase(22);
        boolean b = attributeService.statusUseAndRefresh(attribute, 23L).isSuccess();
        if (b) return actionInfo;

        ////////////////////////////////////////////////////////////////////////////////////////////////////  触发技能效果
        Map<String, List<PvpSkillEffect>> collect = skillEffectService.list(battleId).stream().filter(
                skillEffect -> new BeanUtil.Selector()
                        .eq(actWJ.getId(), skillEffect.getTargetPlayerHeroId())
                        .eq(22, skillEffect.getBaseTrigger().getTrigger3())
                        .value()
        ).collect(Collectors.groupingBy(PvpSkillEffect::obtainSkillInfo));
        collect.forEach((k, v) -> {
            log.info(k);
            List<PvpSkillEffect> pvpSkillEffectList = SkillEffectTriggerHandle.applyNewSkillEffect(v);
            for (PvpSkillEffect pvpSkillEffect : pvpSkillEffectList) {
                actionInfo.skillTriggerInfoList.add(pvpSkillEffect.getSkillEffectTriggerInfo());
            }
        });
        log.info("=======技能触发信息:{}", actionInfo.skillTriggerInfoList);
        ///////////////////////////////////////////////////////////////////////////////////////////////////////  施法技能
        actionInfo.skillReleaseInfoList.addAll(releaseSkillService.heroReleaseSkills(
                battleUUID, actWJ, 1, targetRange
        ));
        log.info("=======技能释放信息:{}", actionInfo.skillReleaseInfoList);
        ///////////////////////////////////////////////////////////////////////////////////////////////////////  普通攻击
        Reason b24 = attributeService.statusUseAndRefresh(attribute, 24L);// 缴械
        actionInfo.setOAReason(b24);
        if (!b24.isSuccess()) {
            b24.setSuccess(false);
            actionInfo.ordinaryAttackList.addAll(ordinaryAttack.apply(battleUUID, actWJ.getId()));
        }
        log.info("=======普通攻击信息:{}", actionInfo.ordinaryAttackList);
        return actionInfo;
    }
}
